#include "FoodView.h"
//#include "FoodListSlider.h"

FoodView::FoodView(Image* fIcon, Image* cIcon, Image* pIcon) :
	fatIcon(fIcon), carbIcon(cIcon), proteinIcon(pIcon), fat(1), carb(1), protein(1), portion(1)
{
	addAndMakeVisible(portionField = new TextField(T("Serving Portion")));
}

FoodView::~FoodView()
{
//	delete slider;
	delete portionField;
}

void FoodView::calculateRatios()
{
	//		this->food = food;
	NutritionalInfo& info = eatable->getNutritionalInfo();
	float total = info.totalFat + info.totalCarb + info.protein;

	this->fat = std::max(0.f, std::min(info.totalFat / total, 1.f));
	this->carb = std::max(0.f, std::min(info.totalCarb / total, 1.f));
	this->protein = std::max(0.f, std::min(info.protein / total, 1.f));
}

void FoodView::resized()
{
//	slider->setBounds(30, getHeight() - 130, 100, 70);
	portionField->setBounds(30, getHeight() - 130, 200, 30);
}

void FoodView::paintExtra(Graphics& g)
{
	int buf = 30;
	int barW = (getWidth() - getImageWidth() - 2 * buf - spacer) / 3;
	Rectangle bottomRight(getImageWidth() + 2 * spacer, getImageWidth() + 2 * spacer, getWidth()
			- (getImageWidth() + 3 * spacer), getHeight() - getImageWidth() - 3 * spacer);

	g.setColour(Colour(250, 254, 250));
	g.fillRect(bottomRight);
	g.setColour(Colours::grey);
	g.drawRect(bottomRight);

	int fatX = getImageWidth() + buf;
	int fatY = int((getHeight() - getImageWidth() - 4 * spacer) * (1 - fat)) + bottomRight.getY() + spacer;
	int carbX = getImageWidth() + buf + barW + spacer;
	int carbY = int((getHeight() - getImageWidth() - 4 * spacer) * (1 - carb)) + bottomRight.getY() + spacer;
	int proteinX = getImageWidth() + buf + 2 * barW + 2 * spacer;
	int proteinY = int((getHeight() - getImageWidth() - 4 * spacer) * (1 - protein)) + bottomRight.getY()
			+ spacer;// + 2 * spacer

	g.setColour(Colours::yellow);
	g.fillRect(fatX, fatY, barW, int((getHeight() - getImageWidth() - 4 * spacer) * fat) - spacer);
	g.setColour(Colours::orange);
	g.fillRect(carbX, carbY, barW, int((getHeight() - getImageWidth() - 4 * spacer) * carb) - spacer);
	g.setColour(Colours::brown);
	g.fillRect(proteinX, proteinY, barW, int((getHeight() - getImageWidth() - 4 * spacer) * protein) - spacer);

	g.drawImageAt(fatIcon, fatX + (barW - fatIcon->getWidth()) / 2, std::max(fatY - fatIcon->getHeight(),
			bottomRight.getY() + 1 * spacer));
	g.drawImageAt(carbIcon, carbX + (barW - carbIcon->getWidth()) / 2, std::max(
			carbY - carbIcon->getHeight(), bottomRight.getY() + 1 * spacer));
	g.drawImageAt(proteinIcon, proteinX + (barW - proteinIcon->getWidth()) / 2, std::max(proteinY
			- proteinIcon->getHeight(), bottomRight.getY() + 1 * spacer));
	Rectangle r(getImageWidth() + 3 * spacer, spacer, getWidth() - getImageWidth() - 3 * spacer,
			getImageWidth());
	MiscGraphics::drawNutritionalDiagram(eatable->getNutritionalInfo(), g, r);
	drawFoodInformation(g);
}

void FoodView::drawFoodInformation(Graphics& g)
{
	Rectangle r(spacer, getImageWidth() + 2 * spacer, getImageWidth(), getHeight() - getImageWidth() - 3
			* spacer);

	Font header(Font(Font::getDefaultSansSerifFontName(), 24, Font::plain));
	Font bold(Font(Font::getDefaultSansSerifFontName(), 16, Font::bold));
	Font plain(Font(Font::getDefaultSansSerifFontName(), 16, Font::plain));

	g.setColour(Colours::white);
	g.fillRect(r);
	g.setColour(Colours::grey);
	g.drawRect(r);

	g.setColour(Colours::black);
	g.setFont(header);
	g.drawMultiLineText(eatable->getName(), spacer + 5, r.getY() + 24, getImageWidth() - 2 * spacer);
	g.setFont(plain);

	ServingAmount serving = interpretServingAmount(eatable->getServingDesc(), eatable->getServingFactor());

	g.drawSingleLineText(String(T("Serving size: ")) + serving.str, spacer + 5, r.getY() + r.getHeight() - 25);
//	g.drawSingleLineText(String::formatted(T("Serving portion: %3.2fg"), portion), spacer
//			+ r.getWidth() / 2, r.getY() + r.getHeight() - 45);
}

ServingAmount FoodView::interpretServingAmount(String& desc, float factor)
{
	string s = string(desc.toUTF8());
	if(s == "no serving specified")
		return ServingAmount(0, T("No Serving Specified"));

	if(s.substr(0, 10) == "yield from") {
		return ServingAmount(0, desc);
	}
	if(s.find("food guide", 0) != string::npos) {
		s = s.substr(23);
		//			s = s.substr(s.find("portion", 0) + 8);
	}

	int first = 100, last = 0;
	for(int i = 0; i < (int) s.size(); i++) {
		if(s[i] <= '9' && s[i] >= '0') {
			first = std::min(first, i);
			break;
		}
	}
	last = first;
	for(int i = first; !(s[i] >= 'a' && s[i] <= 'z') && i < (int) s.size(); i++, last++)
		;

	string sub = s.substr(first, last - first);
	string rem = s.substr(last - first);
	if(sub.find('/', 0) != string::npos) {
		int where = sub.find('/', 0);
		string numer = sub.substr(0, where);
		string denom = sub.substr(where + 1);
		float val = atoi(numer.c_str()) / float(atoi(denom.c_str()));
		rem.insert(0, " ");
		return ServingAmount(val * factor, String::formatted(T("%3.1f"), val * factor) + String(rem.c_str()));
	}

	int val = atoi(sub.c_str());

	stringstream ret;
	ret.setf(std::ios::fixed, std::ios::floatfield);
	ret.precision(1);
	ret << val * factor;
	if(rem.size() > 1)
		ret << " ";
	ret << rem;

	ServingAmount serving(val * factor, String(ret.str().c_str()));
	return serving;
}

void FoodView::setServingPortion(float value)
{
	portion = value;
}
